package org.longteng.mobileluck.algorithm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.longteng.mobileluck.algorithm.bean.MobileNumAuspiciousVo;
import org.longteng.mobileluck.algorithm.bean.MobileNumOrcLuckVo;
import org.longteng.mobileluck.algorithm.service.MobileNumAuspiciousService;
import org.longteng.mobileluck.algorithm.service.SelectMobileNumImgLuckService;
import org.longteng.mobileluck.orc.service.ImageToTextService;
import org.longteng.mobileluck.orc.vo.OrcMobileNumResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 手机选号界面图自动识别手机号吉祥service实现
 * @author hymn.com
 */
@Slf4j
@Service
public class SelectMobileNumImgLuckServiceImpl implements SelectMobileNumImgLuckService {
    @Autowired
    @Qualifier("imageToTextBaiduOrc")
    private ImageToTextService imageToTextService;
    @Autowired
    private MobileNumAuspiciousService mobileNumAuspiciousService;

    /**
     * 通过选号界面图片计算出手机号的吉祥信息
     * @param imageBytes 选号界面图片信息
     * @return
     */
    @Override
    public MobileNumOrcLuckVo parseMobileImgNumsLuck(byte[] imageBytes) {
        MobileNumOrcLuckVo mobileNumOrcLuckVo = new MobileNumOrcLuckVo();
        List<MobileNumAuspiciousVo> mobileNumAuspiciousVoList = null;
        OrcMobileNumResult orcMobileNumResult = imageToTextService.toTextParseMobileNum(imageBytes, ImageToTextService.NumType.MOBILE);
        mobileNumOrcLuckVo.setBaiduOrcApiCountStatus(orcMobileNumResult==null?-1:orcMobileNumResult.getCountStatus());
        // 是否超过API免费调用次数 >=2
        if (orcMobileNumResult.getCountStatus() >=2 ) {
            // 用其他api
        }
        if (orcMobileNumResult != null) {
            List<String> mobileNumList = orcMobileNumResult.getMobileNums();
            if(log.isDebugEnabled()) {
                log.debug("选号界面图片识别出的手机号列表：{}", mobileNumList);
            }
            mobileNumAuspiciousVoList = mobileNumAuspiciousService.parseMobileNumsLuck(mobileNumList);
            mobileNumOrcLuckVo.setMobileNumAuspiciousVoList(mobileNumAuspiciousVoList);
        } else {
            log.warn("选号界面图片识别出结果null.");
        }
        return mobileNumOrcLuckVo;
    }

}
